package com.easyJava.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

public class PropertiesUtils {

    private static final Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);

    // 这是一个工具类，用来读取配置文件
    private static Properties properties = new Properties();


    // 这是一个工具类，用来读取配置文件
    private static Map<String,String> Properties_Map =new ConcurrentHashMap<>();


    //这是用来读取配置文件的方法
    static {
        InputStream is = null;
        try {
            is = PropertiesUtils.class.getClassLoader().getResourceAsStream("application.properties");
            properties.load(is);
            Iterator<Object> iterator = properties.keySet().iterator();
            while (iterator.hasNext()){
                String key = (String) iterator.next();
                String value =properties.getProperty(key);
                Properties_Map.put(key,value);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    logger.error("关闭流失败",e);
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public static String getProperties(String key){
        return Properties_Map.get(key);
    }

    //这是用来测试的
    public static void main(String[] args) {
        System.out.println(getProperties("db.driver.name"));
    }
}
